package document;
 
 
public class ManagingDirectory {
 
	public static final int RECORD_SIZE = 90;
	public static final int BLOCK_SIZE = 90 * 8;
	public static final int FAT_SIZE = 4 * 128;

	
	private Disk disk = new Disk();	
	public void createADirectory(String name, int size) {

		String[] names = name.split("/");
		int blockNumber = 255;
		
		// 寻找是否有目录
		blockNumber = searchDir(name);

		// 得到最后一个目录的的盘块号
		if (blockNumber != 255) {
			// 遍历该盘块，看是否存在同名文件记录
			// i++;
			int blockNumberOfRecord;
			blockNumberOfRecord = disk.searchARecordOfDir(blockNumber,
					names[names.length - 1]);
			// 找不到该文件名，可以增加记录
			if (blockNumberOfRecord == 255) {
				// 在该盘写入一条文件记录
				// 首先，找到一条空记录

				int dir = 8;
				dir = disk.findANullRecord(blockNumber);
				 
				
				int index;
				
				if (dir != 8) {

					// 通过FAT寻找是否有足够的空闲区存储文件
					int[] nullBlocks = new int[size + 1];
					ManagingFAT mf = new ManagingFAT();
					nullBlocks = mf.findTheNullBlocks(size);

					// 如果有足够空间，更新fat表
					if (nullBlocks[0] == size) {
						// 读出fat
						int[] fat = new int[128];
						fat = disk.inputDiskToFat();
						// 更新
						for (int j = 1; j < size; j++) {
							fat[nullBlocks[j]] = nullBlocks[j + 1];
						}

						// 末尾置为255
						fat[nullBlocks[size]] = 255;
						// 写回到新的RecordAllocationTable中
						FileAllocationTable newFat = new FileAllocationTable();
						newFat.setFat(fat);
						disk.inputFatToDisk(newFat);

						// 在该记录块增加一条记录
						Record newRecord = new Record(names[names.length - 1],
								' ', nullBlocks[1], size);
						index = FAT_SIZE + BLOCK_SIZE * (blockNumber - 2) + dir
								* RECORD_SIZE;
						disk.writeADirRecoredToDisk(newRecord, index);
						System.out.println("创建文件夹成功！");

					} else {
						System.out.println("磁盘空间已用完！");
					}

				
				} else {
					System.out.println("该目录（文件夹）记录已满");
				}

			} else {
				System.out.println("该文件夹已存在，不能重复创建！");
			}

		} else {

		}

	}

	public void deleteADir(String name ) {
		 
		String[] names = name.split("/");
		int blockNumber = 255;
		 
		//返回该目录所在的磁盘块号 
		blockNumber = searchDir(name);
 
		 // 得到最后一个目录的的盘块号
		if (blockNumber != 255) {
			// 遍历该盘块，删除同名文件记录
			int blockNumberOfRecord=disk.searchARecordOfDir(blockNumber,names[names.length - 1]);
			
			 
			 if (blockNumberOfRecord != 255) {
				// 在该盘删除这条文件记录，更新fat
			  disk.resetDirRecordFromABlock(blockNumber, names[names.length-1]);
			 
				ManagingFAT mf=new ManagingFAT();
				mf.ResetTheBlocks(blockNumberOfRecord);
				disk.initDirBlock(blockNumberOfRecord);
				 System.out.println("文件夹删除成功");
			}else{
			System.out.println("找不到该文件夹！");
			 
		
		}
	} }

	// 要增加的那条记录的路径是否找得到
	public int searchDir(String name) {

		String[] names = name.split("/");
		int blockNumber = 2;
		int i = 0;
		Disk disk = new Disk();
		if (names[i].equals("Root")) {
			while (blockNumber != 225 && i < names.length - 2){
				i++;
				blockNumber = disk.searchARecordOfDir(blockNumber, names[i]);

				if (blockNumber == 255)
					System.out.print("目录（文件夹）" + "“" + names[i] + "”" + "不存在");

			} 

		} else
			System.out.print("目录（文件夹）" + "“" + names[i] + "”" + "不存在");

		return blockNumber;

	}
	
	 
	
	
	
	public Record[] seeADirContent(String name ) {
		 
		String[] names = name.split("/");
	    Record[] records=new Record[8];
	   
		
		if(names.length==1&&names[0].equals("Root")){
			 records=disk.readRecordsFromABlock(2);
			 for(Record record:records)
				 System.out.println(record);
			
		}
		else{int blockNumber = 255;
		 
		//返回该目录所在的磁盘块号 
		blockNumber = searchDir(name);
 
 
		 // 得到最后一个目录的的盘块号
		if (blockNumber != 255) {
			// 遍历该盘块，删除同名文件记录
			int blockNumberOfRecord=disk.searchARecordOfDir(blockNumber,names[names.length - 1]);
			
			 
			 if (blockNumberOfRecord != 255) {
				 records=disk.readRecordsFromABlock(blockNumberOfRecord);
				 
				 /*for(Record record:records)
					 System.out.println(record);
				 */
			}else{
			System.out.println("找不到该文件夹！");
			 
		
		}
			 
	}
		
}
		return records;}}
 

